//
//  XJComposeController.swift
//  XJWeiBO
//
//  Created by 452608533 on 16/7/26.
//  Copyright © 2016年 452608533. All rights reserved.
//

import UIKit

class XJComposeController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        
    }
    
    func setupUI() {
        view.backgroundColor = UIColor.whiteColor()
        setupNav()
        //添加控件
        view.addSubview(composeTextView)
        composeTextView.addSubview(pictureView)
        view.addSubview(toolBar)
        //设置约束
        composeTextView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(view).offset(UIEdgeInsetsZero)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: SCREENW - 20, height: SCREENW - 20))
            make.top.equalTo(composeTextView).offset(100)
            make.centerX.equalTo(composeTextView)
        }
        
        toolBar.snp_makeConstraints { (make) -> Void in
            make.height.equalTo(44)
            make.bottom.leading.trailing.equalTo(view)
        }
        
        toolBar.closure = {(type: XJComposeToolBarType)->() in
            
            switch type{
            case .Picture:
                print("图片")
            case .Mention:
                print("@")
            case .Trend:
                print("#")
            case .Emoticon:
                print("笑脸")
            case .Add:
                print("+")
            }
        }
    }
    
    
    
    func setupNav() {
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", imgName: nil, target: self, action: "cancelAction")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发布", imgName: nil, target: self, action: "sendAction")
        navigationItem.rightBarButtonItem?.enabled = false
        navigationItem.titleView = titleView
    }
    //取消
    func cancelAction() {
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    func sendAction() {
        print("点击发布按钮")
    }
    
    //懒加载控件
    private lazy var titleView: UILabel = {
        //获取用户的名字
        let name = XJUserAccountViewModel.sharedTools.userAccount?.screen_name ?? ""
        
        let str = "发微博\n\(name)"
        //获取范围
        let range = (str as NSString).rangeOfString(name)
        
        let attr = NSMutableAttributedString(string: str)
        
        attr.addAttributes([NSForegroundColorAttributeName: UIColor.orangeColor(), NSFontAttributeName:UIFont.systemFontOfSize(12)], range: range)
        
        let label = UILabel()
        label.attributedText = attr
        label.textAlignment = .Center
        label.numberOfLines = 0
        //必须写上  不然label不能显示
        label.sizeToFit()
        
        return label
    }()
    //自定义textView
    private lazy var composeTextView:XJComposeTextView = {
        
        let compose = XJComposeTextView()
        compose.font = UIFont.systemFontOfSize(15)
        compose.placeholder = "君埋泉下泥销骨,我寄人间雪满头,阿卫韩郎相次去,夜台茫昧得知不"
        compose.alwaysBounceVertical = true
        compose.delegate = self
        return compose
    }()
    
    private lazy var pictureView: XJComposePictureView = XJComposePictureView()
    private lazy var toolBar: XJComposeToolBar = XJComposeToolBar()
}


extension XJComposeController: UITextViewDelegate{
    //当鼠标点击文本框的时候调用这个方法
//    func textViewDidBeginEditing(textView: UITextView) {
//       
//    }
    
    func textViewDidChange(textView: UITextView) {
        
   navigationItem.rightBarButtonItem?.enabled = textView.hasText()
    }
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        self.composeTextView.resignFirstResponder()
    }
}



















